package com.the_ring.chapter06.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.the_ring.chapter06.R;
import com.the_ring.chapter06.bean.GoodsInfo;

import java.util.List;

public class CartItemAdapter extends BaseAdapter {

    private Context mContext;


    private List<GoodsInfo> infoList;

    public CartItemAdapter(Context context, List<GoodsInfo> infoList) {
        mContext = context;
        this.infoList = infoList;
    }

    @Override
    public int getCount() {
        return infoList.size();
    }

    @Override
    public Object getItem(int position) {
        return infoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.cart_item, null);
            holder.phoneIv = convertView.findViewById(R.id.iv_phone);
            holder.nameTv = convertView.findViewById(R.id.tv_name);
            holder.detailTv = convertView.findViewById(R.id.tv_detail);
            holder.numTv = convertView.findViewById(R.id.tv_count);
            holder.priceTv = convertView.findViewById(R.id.tv_price);
            holder.totalTv = convertView.findViewById(R.id.tv_total);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        GoodsInfo info = infoList.get(position);
        holder.phoneIv.setImageURI(Uri.parse(info.picPath));
        holder.nameTv.setText(info.name);
        holder.detailTv.setText(info.detail);
        holder.numTv.setText(String.valueOf(info.num));
        holder.priceTv.setText(String.valueOf(info.price));
        holder.totalTv.setText(String.valueOf(info.num * info.price));

        return convertView;
    }

    public class ViewHolder {
        ImageView phoneIv;
        TextView nameTv;
        TextView detailTv;
        TextView numTv;
        TextView priceTv;
        TextView totalTv;
    }
}
